package com.example.springboot.service;

import com.example.springboot.init.MultiJdbcConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 多数据源查询测试
 * <p>
 * <p>
 *
 * @author Zhang.Z
 * @version 2025-09-22
 */
@Service
@ConditionalOnBean(MultiJdbcConfig.class)
public class MultiService {

    private final JdbcTemplate primaryJdbc;

    private final JdbcTemplate secondaryJdbc;


    public MultiService(@Qualifier("primaryJdbcTemplate") JdbcTemplate primaryJdbc,
                        @Qualifier("secondaryJdbcTemplate") JdbcTemplate secondaryJdbc) {
        this.primaryJdbc = primaryJdbc;
        this.secondaryJdbc = secondaryJdbc;
    }

    public void findCount() {
        Integer count = primaryJdbc.queryForObject("select count(*) from `t_user_tab`", Integer.class);
        System.out.println("primary user size = " + count);

        Integer count2 = secondaryJdbc.queryForObject("select count(*) from `t_user_tab`", Integer.class);
        System.out.println("second user size = " + count2);
    }

}
